package service

import (
	"dataaysn/internal/model"
	"dataaysn/internal/repository"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type HospitalizationDetailService struct {
	repo repository.HospitalizationDetailRepository
}

func NewHospitalizationDetailService(repo repository.HospitalizationDetailRepository) *HospitalizationDetailService {
	return &HospitalizationDetailService{repo: repo}
}

func (s *HospitalizationDetailService) CreateDetail(c *gin.Context) {
	var detail model.HospitalizationDetail
	if err := c.ShouldBindJSON(&detail); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}

	if err := s.repo.Create(&detail); err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}

	c.JSON(200, detail)
}

func (s *HospitalizationDetailService) BatchCreateDetails(c *gin.Context) {
	var details []model.HospitalizationDetail
	if err := c.ShouldBindJSON(&details); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}

	if err := s.repo.BatchCreate(details); err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}

	c.JSON(200, gin.H{"message": "batch create successful"})
}

func (s *HospitalizationDetailService) GetDetailByID(c *gin.Context) {
	id := c.Param("id")
	idUint, errConv := strconv.ParseUint(id, 10, 64)
	if errConv != nil {
		// 转换失败，返回错误响应
		c.JSON(400, gin.H{"error": "invalid id format"})
		return
	}
	detail, err := s.repo.GetByID(uint(idUint))
	if err != nil {
		c.JSON(404, gin.H{"error": "not found"})
		return
	}
	c.JSON(200, detail)
}

func (s *HospitalizationDetailService) ListDetailsByInpatient(c *gin.Context) {
	inpatientNo := c.Param("inpatientNo")
	details, err := s.repo.ListByInpatientNo(inpatientNo)
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}
	c.JSON(200, details)
}

func (s *HospitalizationDetailService) ListDetailsByDateRange(c *gin.Context) {
	hospitalCode := c.Param("hospitalCode")
	startDateStr := c.Query("startDate")
	endDateStr := c.Query("endDate")

	startDate, err := time.Parse("2006-01-02", startDateStr)
	if err != nil {
		c.JSON(400, gin.H{"error": "invalid start date format"})
		return
	}

	endDate, err := time.Parse("2006-01-02", endDateStr)
	if err != nil {
		c.JSON(400, gin.H{"error": "invalid end date format"})
		return
	}

	details, err := s.repo.ListByHospitalAndDate(hospitalCode, startDate, endDate)
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}
	c.JSON(200, details)
}

func (s *HospitalizationDetailService) Delete(c *gin.Context) {
	id := c.Param("id")
	idUint, errConv := strconv.ParseUint(id, 10, 64)
	if errConv != nil {
		// 转换失败，返回错误响应
		c.JSON(400, gin.H{"error": "invalid id format"})
		return
	}
	if err := s.repo.DeleteById(uint(idUint)); err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}
	c.JSON(200, gin.H{"message": "deleted"})
}

func (s *HospitalizationDetailService) UpdateDetail(c *gin.Context) {
	var detail model.HospitalizationDetail
	if err := c.ShouldBindJSON(&detail); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}

	if err := s.repo.UpdateDetail(&detail); err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
		return
	}

	c.JSON(200, detail)
}
